<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户钱包地址" prop="walletAddress">
        <el-input
          v-model="queryParams.walletAddress"
          placeholder="请输入用户钱包地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="币种符号" prop="tokenSymbol">
        <el-input
          v-model="queryParams.tokenSymbol"
          placeholder="请输入币种符号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="网络类型" prop="network">
        <el-input
          v-model="queryParams.network"
          placeholder="请输入网络类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付币种数量" prop="amountPaid">
        <el-input
          v-model="queryParams.amountPaid"
          placeholder="请输入支付币种数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="兑换的bluebera数量" prop="blueberaAmount">
        <el-input
          v-model="queryParams.blueberaAmount"
          placeholder="请输入兑换的bluebera数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="兑换bluebera汇率" prop="rateToBluebera">
        <el-input
          v-model="queryParams.rateToBluebera"
          placeholder="请输入兑换bluebera汇率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="链上交易哈希" prop="txHash">
        <el-input
          v-model="queryParams.txHash"
          placeholder="请输入链上交易哈希"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单逻辑状态" prop="status">
        <el-input
          v-model="queryParams.status"
          placeholder="请输入订单逻辑状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="链上交易状态" prop="txStatus">
        <el-input
          v-model="queryParams.txStatus"
          placeholder="请输入链上交易状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上次链上扫描时间">
        <el-date-picker
          v-model="daterangeScanLastChecked"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreatedAt"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bluebera:orders:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bluebera:orders:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bluebera:orders:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bluebera:orders:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="用户钱包地址" align="center" prop="walletAddress" />
      <el-table-column label="币种符号" align="center" prop="tokenSymbol" />
      <el-table-column label="网络类型" align="center" prop="network" />
      <el-table-column label="支付币种数量" align="center" prop="amountPaid" />
      <el-table-column label="兑换的bluebera数量" align="center" prop="blueberaAmount" />
      <el-table-column label="兑换bluebera汇率" align="center" prop="rateToBluebera" />
      <el-table-column label="链上交易哈希" align="center" prop="txHash" />
      <el-table-column label="订单逻辑状态" align="center" prop="status" />
      <el-table-column label="链上交易状态" align="center" prop="txStatus" />
      <el-table-column label="区块高度" align="center" prop="blockNumber" />
      <el-table-column label="确认数" align="center" prop="confirmations" />
      <el-table-column label="Gas使用量" align="center" prop="gasUsed" />
      <el-table-column label="上次链上扫描时间" align="center" prop="scanLastChecked" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scanLastChecked, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="链上扫描出错信息" align="center" prop="scanError" />
      <el-table-column label="验证信息" align="center" prop="nonce" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bluebera:orders:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bluebera:orders:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户钱包地址" prop="walletAddress">
          <el-input v-model="form.walletAddress" placeholder="请输入用户钱包地址" />
        </el-form-item>
        <el-form-item label="币种符号" prop="tokenSymbol">
          <el-input v-model="form.tokenSymbol" placeholder="请输入币种符号" />
        </el-form-item>
        <el-form-item label="网络类型" prop="network">
          <el-input v-model="form.network" placeholder="请输入网络类型" />
        </el-form-item>
        <el-form-item label="支付币种数量" prop="amountPaid">
          <el-input v-model="form.amountPaid" placeholder="请输入支付币种数量" />
        </el-form-item>
        <el-form-item label="兑换的bluebera数量" prop="blueberaAmount">
          <el-input v-model="form.blueberaAmount" placeholder="请输入兑换的bluebera数量" />
        </el-form-item>
        <el-form-item label="兑换bluebera汇率" prop="rateToBluebera">
          <el-input v-model="form.rateToBluebera" placeholder="请输入兑换bluebera汇率" />
        </el-form-item>
        <el-form-item label="链上交易哈希" prop="txHash">
          <el-input v-model="form.txHash" placeholder="请输入链上交易哈希" />
        </el-form-item>
        <el-form-item label="订单逻辑状态" prop="status">
          <el-input v-model="form.status" placeholder="请输入订单逻辑状态" />
        </el-form-item>
        <el-form-item label="链上交易状态" prop="txStatus">
          <el-input v-model="form.txStatus" placeholder="请输入链上交易状态" />
        </el-form-item>
        <el-form-item label="区块高度" prop="blockNumber">
          <el-input v-model="form.blockNumber" placeholder="请输入区块高度" />
        </el-form-item>
        <el-form-item label="确认数" prop="confirmations">
          <el-input v-model="form.confirmations" placeholder="请输入确认数" />
        </el-form-item>
        <el-form-item label="Gas使用量" prop="gasUsed">
          <el-input v-model="form.gasUsed" placeholder="请输入Gas使用量" />
        </el-form-item>
        <el-form-item label="上次链上扫描时间" prop="scanLastChecked">
          <el-date-picker clearable
            v-model="form.scanLastChecked"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上次链上扫描时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="链上扫描出错信息" prop="scanError">
          <el-input v-model="form.scanError" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="验证信息" prop="nonce">
          <el-input v-model="form.nonce" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/bluebera/orders"

export default {
  name: "Orders",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户订单表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 更新时间时间范围
      daterangeScanLastChecked: [],
      // 更新时间时间范围
      daterangeCreatedAt: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        walletAddress: null,
        tokenSymbol: null,
        network: null,
        amountPaid: null,
        blueberaAmount: null,
        rateToBluebera: null,
        txHash: null,
        status: null,
        txStatus: null,
        scanLastChecked: null,
        createdAt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        walletAddress: [
          { required: true, message: "用户钱包地址不能为空", trigger: "blur" }
        ],
        tokenSymbol: [
          { required: true, message: "币种符号不能为空", trigger: "blur" }
        ],
        amountPaid: [
          { required: true, message: "支付币种数量不能为空", trigger: "blur" }
        ],
        blueberaAmount: [
          { required: true, message: "兑换的bluebera数量不能为空", trigger: "blur" }
        ],
        rateToBluebera: [
          { required: true, message: "兑换bluebera汇率不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "订单逻辑状态不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户订单列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeScanLastChecked && '' != this.daterangeScanLastChecked) {
        this.queryParams.params["beginScanLastChecked"] = this.daterangeScanLastChecked[0]
        this.queryParams.params["endScanLastChecked"] = this.daterangeScanLastChecked[1]
      }
      if (null != this.daterangeCreatedAt && '' != this.daterangeCreatedAt) {
        this.queryParams.params["beginCreatedAt"] = this.daterangeCreatedAt[0]
        this.queryParams.params["endCreatedAt"] = this.daterangeCreatedAt[1]
      }
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        walletAddress: null,
        tokenSymbol: null,
        network: null,
        amountPaid: null,
        blueberaAmount: null,
        rateToBluebera: null,
        txHash: null,
        status: null,
        txStatus: null,
        blockNumber: null,
        confirmations: null,
        gasUsed: null,
        scanLastChecked: null,
        scanError: null,
        nonce: null,
        createdAt: null,
        updatedAt: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeScanLastChecked = []
      this.daterangeCreatedAt = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加用户订单"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getOrders(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改用户订单"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除用户订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrders(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bluebera/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
